package com.senven.books.modules.system.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senven.books.modules.system.log.entity.SysLog;
import com.senven.books.modules.system.log.mapper.SysLogMapper;
import com.senven.books.modules.system.log.service.ISysLogService;
import com.senven.books.mybatis.baseEntity.SimpleEntity;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author longshengyi
 * @since 2021-08-13
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;


    @Override
    public IPage<SysLog> listLogPage(Page<SysLog> page, SysLog model) {
        LambdaQueryWrapper<SysLog> queryWrapper = this.getSysLogLambdaQueryWrapper(model);
        return sysLogMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<SysLog> listByQuery(SysLog sysLog) {
        LambdaQueryWrapper<SysLog> queryWrapper = this.getSysLogLambdaQueryWrapper(sysLog);
        return this.list(queryWrapper);
    }

    private LambdaQueryWrapper<SysLog> getSysLogLambdaQueryWrapper(SysLog model) {
        LambdaQueryWrapper<SysLog> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotEmpty(model.getCategory())) {
            queryWrapper.eq(SysLog::getCategory, model.getCategory());
        }


        if (StringUtils.isNotEmpty(model.getContent())) {
            queryWrapper.like(SysLog::getContent, model.getContent());
        }


        if (StringUtils.isNotBlank(model.getUserName())) {
            queryWrapper.like(SysLog::getUserName, model.getUserName());
        }

        queryWrapper.orderByDesc(SimpleEntity::getCreateTime);
        return queryWrapper;
    }
}
